package com.zou.controller.backstage;

import com.zou.commons.constants.Constants;
import com.zou.commons.domain.ReturnObject;
import com.zou.pojo.Column;
import com.zou.pojo.Manager;
import com.zou.service.ColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ColumnController {
    @Autowired
    private ColumnService columnService;

    /**
     * 进入栏目列表
     *
     * @return
     */
    @GetMapping("/backstage/column/list")
    public String list() {
        return "backstage/column/list.html";
    }

    /**
     * 进入栏目添加页
     *
     * @return
     */
    @GetMapping("/backstage/column/add")
    public String add() {
        return "/backstage/column/add";
    }

    /**
     * 添加栏目并返回响应信息
     *
     * @param column
     * @param session
     * @return
     */
    @PostMapping("/backstage/column/add")
    public @ResponseBody
    Object add(Column column, HttpSession session) {
        // 获取当前操作人
        Manager manager = (Manager) session.getAttribute(Constants.SESSION_USER);
        column.setManagerId(manager.getManagerId());
        // 调用service层方法保存栏目
        ReturnObject returnObject = new ReturnObject();
        if (columnService.saveColumn(column)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 批量删除栏目
     *
     * @param arr
     * @return
     */
    @PostMapping("/backstage/column/del")
    public @ResponseBody
    Object del(String[] arr) {
        ReturnObject returnObject = new ReturnObject();
        if (columnService.deleteColumnByIds(arr)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 根据id查询对应栏目，进入修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/backstage/column/edit/{id}")
    public String edit(@PathVariable String id, Model model) {
        Column column = columnService.queryColumnById(id);
        model.addAttribute("column", column);
        return "backstage/column/edit.html";
    }

    /**
     * 修改栏目信息
     *
     * @param column
     * @return
     */
    @PostMapping("/backstage/column/edit")
    public @ResponseBody
    Object edit(Column column) {
        ReturnObject returnObject = new ReturnObject();
        if (columnService.editColumn(column)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 置顶栏目
     *
     * @param id
     * @return
     */
    @PostMapping("/backstage/column/sticky")
    public @ResponseBody
    Object sticky(String id) {
        ReturnObject returnObject = new ReturnObject();
        if (columnService.stickyColumn(id)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }


    /**
     * 根据条件分页查询
     *
     * @param state
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/backstage/column/queryColumnByConditionForPage")
    public @ResponseBody
    Object queryColumnByConditionForPage(String state, String name, int pageNo, int pageSize) {
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("state", state);
        map.put("name", name);
        map.put("beginNo", (pageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        //调用service层方法，查询数据
        List<Column> columns = columnService.queryColumnByConditionForPage(map);
        int totalRows = columnService.queryCountOfColumnByCondition(map);
        //根据查询结果结果，生成响应信息
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("columns", columns);
        retMap.put("totalRows", totalRows);
        return retMap;
    }
}
